﻿using System;

namespace Smatime.Web
{
    /// <summary>
    ///     Contain all common utility functions to process data conversion
    /// </summary>
    public static class DataHelper
    {
        public const int NotSet = -1;
        /// <summary>
        ///     Get TimeSpan value from src object. Return null if the src is null or DBNUll
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static TimeSpan? ToTimeSpan(this object src)
        {
            if (src == null)
                return null;
            if (src == DBNull.Value)
                return null;
            return (TimeSpan) src;
        }
        /// <summary>
        ///     Get DateTime value from src object. Return null if the src is null or DBNUll
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(this object src)
        {
            if (src == null)
                return null;
            if (src == DBNull.Value)
                return null;
            return (DateTime) src;
        }
        public static object ToRowValue(this object src)
        {
            if (src == null)
                return DBNull.Value;
            return src;
        }
        public static bool IsNull(this object src)
        {
            return (src == null) || (src.Equals(DBNull.Value));
        }

        public static bool HasValue(this object src)
        {
            return (src != null) && (!src.Equals(DBNull.Value)) && (!src.ToString().Equals("-1"));
        }
    }
}